package com.sky.controller.admin;

import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.nio.channels.Channel;
import java.util.List;

@RestController("adminSetmealController")
@Slf4j
@Api(tags = "套餐管理")
public class SetmealController {
    @Autowired
    private SetmealService setmealService;

    @PostMapping("admin/setmeal")
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")//精确清理
    public Result AddSetMeal(@RequestBody SetmealDTO setmealDTO){
        log.info("新增套餐");
        setmealService.addSetMealWithDish(setmealDTO);
        return Result.success();
    }

    @GetMapping("/admin/setmeal/page")
    @ApiOperation("套餐分页查询")
    public Result<PageResult> see(SetmealPageQueryDTO setmealPageQueryDTO){
        log.info("分页查询");
        PageResult pageResult=setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }

    @DeleteMapping("/admin/setmeal")
    @ApiOperation("批量删除套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result deleteSetmealBatch(@RequestParam List<Long> ids){
        log.info("批量删除套餐");
        setmealService.deleteSetmealBatch(ids);
        return Result.success();
    }

    @GetMapping("/admin/setmeal/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> seeById(@PathVariable Long id){
        log.info("根据id查询套餐");
        SetmealVO setmealVO=setmealService.getByIdWithDish(id);
        return Result.success(setmealVO);
    }

    @PutMapping("/admin/setmeal")
    @ApiOperation("修改套餐")
    public Result updateSetmeal(@RequestBody SetmealDTO setmealDTO){
        log.info("修改套餐的基本信息");
        setmealService.updateSetmeal(setmealDTO);
        return Result.success();
    }

    @PostMapping("/admin/setmeal/status/{status}")
    @ApiOperation("套餐的起售停售")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result<String> startOrStop(@PathVariable Integer status, Long id){
        log.info("套餐的起售停售");
        setmealService.startOrStop(status,id);
        return Result.success();
    }

}
